package com.livestocksolutions.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

/**
 * Log messages to both the Screen and to a File
 * @author Adam Parr
 * @.modified 18 January 2012
 */
public class Logger extends PrintStream {
	
	/**
	 * The PrintStream that will print to a console or screen.
	 */
	protected PrintStream printStream;
	
	/**
	 * Constructor to print messages to alternative sources.
	 * @param outputPrinter The Printer that will print to a File.
	 * @param alternatePrinter The Printer that will print to a console or screen.
	 */
	public Logger(OutputStream outputPrinter, PrintStream alternatePrinter){
		super(outputPrinter);
		printStream=alternatePrinter;
		try {
			outputPrinter.write("\r\n\r\n".getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
    public void flush() {
        super.flush();
        printStream.flush();
    }

    @Override
    public void write(byte[] buf, int off, int len) {
        super.write(buf, off, len);
        printStream.write(buf, off, len);
    }

    @Override
    public void write(int b) {
        super.write(b);
        printStream.write(b);
    }

    @Override
    public void write(byte[] b) throws IOException {
        super.write(b);
        printStream.write(b);
    }
}
